package web;
import pojo.SmbmsBill;
import pojo.SmbmsUser;
import service.SmbmsBillService;
import service.SmbmsUserService;
import service.impl.SmbmsBillServiceImpl;
import service.impl.SmbmsUserServiceImpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet(value = "/addBillServlet")
public class AddBillServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        this.doPost(req, resp);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        SmbmsBill smbmsBill=new SmbmsBill();
//从界面获取数据
//订单编码
        String billcode=req.getParameter("billCode");
//商品名称
        String productname=req.getParameter("productName");
//商品单位
        String productunit=req.getParameter("productUnit");
//商品数量
        String productcount=req.getParameter("productCount");

        try {
//总金额
            String totalprice=req.getParameter("totalPrice");
//供应商（待编写）
            String providerId=req.getParameter("providerId");
            int providerid=Integer.parseInt(providerId);
//是否付款
            String sisPayment=req.getParameter("isPayment");
            System.out.println("isPayment:"+sisPayment+"-------------------------------------");
            int ispayment=Integer.parseInt(sisPayment);

            System.out.println("isPayment:"+sisPayment+"-------------------------------------");
            smbmsBill.setBillCode(billcode);
            smbmsBill.setProductName(productname);
            smbmsBill.setProductUnit(productunit);
            smbmsBill.setProductCount(productcount);
            smbmsBill.setTotalPrice(totalprice);
            smbmsBill.setProviderId(providerid);
            smbmsBill.setIsPayment(ispayment);
//创建人, 当前登录人 怎么获取 session
            HttpSession session=req.getSession();
            SmbmsUser smbmsUser = (SmbmsUser) session.getAttribute("userSession");
            smbmsBill.setCreatedBy(smbmsUser.getId());
//当前时间
            smbmsBill.setCreationDate(new Date());
//调用业务层接口
            SmbmsBillService smbmsBillService=new SmbmsBillServiceImpl();
            int i = smbmsBillService.addBill(smbmsBill);
            if(i>0){
//添加成功
                req.getRequestDispatcher("/BillListServlet").forward(req,resp);
            }else{
//添加失败
                resp.sendRedirect("BillAdd.jsp");
            }
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
